package mapProperties;

import acm.graphics.GObject;
import arcturus.Game;
import arcturus.Map;


public class BulletEntity extends Entity implements Runnable{
	public int xorgion = 0;
	public int yorgion = 0;
	public int xintercept = 0;
	public int yintercept = 0;
	private Game g;
	public boolean runthis = true;	
	private Map currentMap;
	
	
	public BulletEntity( int xorgion, int yorgion, int xintercept, int yintercept, String picLocation, Game g, Map currentMap){
		super(50, PASS_INDX_IMPASSABLE, picLocation);
		this.xorgion = xorgion;
		this.yorgion = yorgion;
		this.x = xorgion;
		this.y = yorgion;
		this.xintercept = xintercept;
		this.yintercept = yintercept;
		this.g = g;	
		this.currentMap = currentMap;
		g.add(this, this.x, this.y);
	}
	
	
	
	//====================================
	//====================================
	//====================================
	
	public boolean isRunable(){
		return this.runthis;
	}


	@Override
	public void run() {		
		g.remove(this);
		GObject temp;
		//System.out.println("Bullet Running");
		if(this.runthis){
		//	System.out.println("Bullet Running true");
			this.adjustPosition();			
			this.g.add(this, this.x, this.y);
			if(this.currentMap.isColliding(((int)x), ((int)y))){
				temp = (GObject) this.currentMap.getValueAt(((int)x), ((int)y));			
				if(temp!= null && !temp.equals(this.currentMap.theHuman)){						
					this.g.remove(temp);
					//this.currentMap.remove(String.valueOf(temp.hashCode()));
				}
			}
			//System.out.println(this.x + " " + this.y);
		}
	}
	//====================================
		//====================================
		//====================================
	private void adjustPosition(){
		boolean x =  this.adjustX();
		boolean y = this.adjustY();
		this.runthis = ((x) || (y));
	}
	//====================================
		//====================================
		//====================================
	private boolean adjustX(){
		boolean rval = (((int)this.x) != this.xintercept);		
		if(this.xorgion <= this.xintercept && rval){
			this.x++;
		}
		else if(rval){
			this.x--;
		}		
		return rval;
	}
	//====================================
		//====================================
		//====================================
	private boolean adjustY(){
		boolean rval = (((int)this.y) != this.yintercept);
		
		if(this.yorgion <= this.yintercept && rval){
			this.y++;
			
		}
		else if(rval){
			this.y--;
		}
		
		return rval;
	}
	//====================================
		//====================================
		//====================================
}
